home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / sh03src.zoo / sh-pl03 / sh / nodes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-22  |  8.7 KB  |  344 lines

  1. /*
  2.  * This file was generated by the mknodes program.
  3.  */
  4.  
  5. /*-
  6.  * Copyright (c) 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * This code is derived from software contributed to Berkeley by
  10.  * Kenneth Almquist.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or without
  13.  * modification, are permitted provided that the following conditions
  14.  * are met:
  15.  * 1. Redistributions of source code must retain the above copyright
  16.  *    notice, this list of conditions and the following disclaimer.
  17.  * 2. Redistributions in binary form must reproduce the above copyright
  18.  *    notice, this list of conditions and the following disclaimer in the
  19.  *    documentation and/or other materials provided with the distribution.
  20.  * 3. All advertising materials mentioning features or use of this software
  21.  *    must display the following acknowledgement:
  22.  *    This product includes software developed by the University of
  23.  *    California, Berkeley and its contributors.
  24.  * 4. Neither the name of the University nor the names of its contributors
  25.  *    may be used to endorse or promote products derived from this software
  26.  *    without specific prior written permission.
  27.  *
  28.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38.  * SUCH DAMAGE.
  39.  *
  40.  *    @(#)nodes.c.pat    5.2 (Berkeley) 3/8/91
  41.  */
  42.  
  43. /*
  44.  * Routine for dealing with parsed shell commands.
  45.  */
  46.  
  47. #include "shell.h"
  48. #include "nodes.h"
  49. #include "memalloc.h"
  50. #include "machdep.h"
  51. #include "mystring.h"
  52.  
  53.  
  54. int funcblocksize;        /* size of structures in function */
  55. int funcstringsize;        /* size of strings in node */
  56. #ifdef __STDC__
  57. pointer funcblock;        /* block to allocate function from */
  58. #else
  59. char *funcblock;        /* block to allocate function from */
  60. #endif
  61. char *funcstring;        /* block to allocate strings from */
  62.  
  63. static const short nodesize[23] = {
  64.       ALIGN(sizeof (struct nbinary)),
  65.       ALIGN(sizeof (struct ncmd)),
  66.       ALIGN(sizeof (struct npipe)),
  67.       ALIGN(sizeof (struct nredir)),
  68.       ALIGN(sizeof (struct nredir)),
  69.       ALIGN(sizeof (struct nredir)),
  70.       ALIGN(sizeof (struct nbinary)),
  71.       ALIGN(sizeof (struct nbinary)),
  72.       ALIGN(sizeof (struct nif)),
  73.       ALIGN(sizeof (struct nbinary)),
  74.       ALIGN(sizeof (struct nbinary)),
  75.       ALIGN(sizeof (struct nfor)),
  76.       ALIGN(sizeof (struct ncase)),
  77.       ALIGN(sizeof (struct nclist)),
  78.       ALIGN(sizeof (struct narg)),
  79.       ALIGN(sizeof (struct narg)),
  80.       ALIGN(sizeof (struct nfile)),
  81.       ALIGN(sizeof (struct nfile)),
  82.       ALIGN(sizeof (struct nfile)),
  83.       ALIGN(sizeof (struct ndup)),
  84.       ALIGN(sizeof (struct ndup)),
  85.       ALIGN(sizeof (struct nhere)),
  86.       ALIGN(sizeof (struct nhere)),
  87. };
  88.  
  89.  
  90. #ifdef __STDC__
  91. STATIC void calcsize(union node *);
  92. STATIC void sizenodelist(struct nodelist *);
  93. STATIC union node *copynode(union node *);
  94. STATIC struct nodelist *copynodelist(struct nodelist *);
  95. STATIC char *nodesavestr(char *);
  96. #else
  97. STATIC void calcsize();
  98. STATIC void sizenodelist();
  99. STATIC union node *copynode();
  100. STATIC struct nodelist *copynodelist();
  101. STATIC char *nodesavestr();
  102. #endif
  103.  
  104.  
  105.  
  106. /*
  107.  * Make a copy of a parse tree.
  108.  */
  109.  
  110. union node *
  111. copyfunc(n)
  112.       union node *n;
  113.       {
  114.       if (n == NULL)
  115.         return NULL;
  116.       funcblocksize = 0;
  117.       funcstringsize = 0;
  118.       calcsize(n);
  119.       funcblock = ckmalloc(funcblocksize + funcstringsize);
  120.       funcstring = funcblock + funcblocksize;
  121.       return copynode(n);
  122. }
  123.  
  124.  
  125.  
  126. STATIC void
  127. calcsize(n)
  128.       union node *n;
  129.       {
  130.       if (n == NULL)
  131.         return;
  132.       funcblocksize += nodesize[n->type];
  133.       switch (n->type) {
  134.       case NSEMI:
  135.       case NAND:
  136.       case NOR:
  137.       case NWHILE:
  138.       case NUNTIL:
  139.         calcsize(n->nbinary.ch2);
  140.         calcsize(n->nbinary.ch1);
  141.         break;
  142.       case NCMD:
  143.         calcsize(n->ncmd.redirect);
  144.         calcsize(n->ncmd.args);
  145.         break;
  146.       case NPIPE:
  147.         sizenodelist(n->npipe.cmdlist);
  148.         break;
  149.       case NREDIR:
  150.       case NBACKGND:
  151.       case NSUBSHELL:
  152.         calcsize(n->nredir.redirect);
  153.         calcsize(n->nredir.n);
  154.         break;
  155.       case NIF:
  156.         calcsize(n->nif.elsepart);
  157.         calcsize(n->nif.ifpart);
  158.         calcsize(n->nif.test);
  159.         break;
  160.       case NFOR:
  161.         funcstringsize += strlen(n->nfor.var) + 1;
  162.         calcsize(n->nfor.body);
  163.         calcsize(n->nfor.args);
  164.         break;
  165.       case NCASE:
  166.         calcsize(n->ncase.cases);
  167.         calcsize(n->ncase.expr);
  168.         break;
  169.       case NCLIST:
  170.         calcsize(n->nclist.body);
  171.         calcsize(n->nclist.pattern);
  172.         calcsize(n->nclist.next);
  173.         break;
  174.       case NDEFUN:
  175.       case NARG:
  176.         sizenodelist(n->narg.backquote);
  177.         funcstringsize += strlen(n->narg.text) + 1;
  178.         calcsize(n->narg.next);
  179.         break;
  180.       case NTO:
  181.       case NFROM:
  182.       case NAPPEND:
  183.         calcsize(n->nfile.fname);
  184.         calcsize(n->nfile.next);
  185.         break;
  186.       case NTOFD:
  187.       case NFROMFD:
  188.         calcsize(n->ndup.next);
  189.         break;
  190.       case NHERE:
  191.       case NXHERE:
  192.         calcsize(n->nhere.doc);
  193.         calcsize(n->nhere.next);
  194.         break;
  195.       };
  196. }
  197.  
  198.  
  199.  
  200. STATIC void
  201. sizenodelist(lp)
  202.       struct nodelist *lp;
  203.       {
  204.       while (lp) {
  205.         funcblocksize += ALIGN(sizeof (struct nodelist));
  206.         calcsize(lp->n);
  207.         lp = lp->next;
  208.       }
  209. }
  210.  
  211.  
  212.  
  213. STATIC union node *
  214. copynode(n)
  215.       union node *n;
  216.       {
  217.       union node *new;
  218.  
  219.       if (n == NULL)
  220.         return NULL;
  221.       new = funcblock;
  222.       funcblock += nodesize[n->type];
  223.       switch (n->type) {
  224.       case NSEMI:
  225.       case NAND:
  226.       case NOR:
  227.       case NWHILE:
  228.       case NUNTIL:
  229.         new->nbinary.ch2 = copynode(n->nbinary.ch2);
  230.         new->nbinary.ch1 = copynode(n->nbinary.ch1);
  231.         break;
  232.       case NCMD:
  233.         new->ncmd.redirect = copynode(n->ncmd.redirect);
  234.         new->ncmd.args = copynode(n->ncmd.args);
  235.         new->ncmd.backgnd = n->ncmd.backgnd;
  236.         break;
  237.       case NPIPE:
  238.         new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
  239.         new->npipe.backgnd = n->npipe.backgnd;
  240.         break;
  241.       case NREDIR:
  242.       case NBACKGND:
  243.       case NSUBSHELL:
  244.         new->nredir.redirect = copynode(n->nredir.redirect);
  245.         new->nredir.n = copynode(n->nredir.n);
  246.         break;
  247.       case NIF:
  248.         new->nif.elsepart = copynode(n->nif.elsepart);
  249.         new->nif.ifpart = copynode(n->nif.ifpart);
  250.         new->nif.test = copynode(n->nif.test);
  251.         break;
  252.       case NFOR:
  253.         new->nfor.var = nodesavestr(n->nfor.var);
  254.         new->nfor.body = copynode(n->nfor.body);
  255.         new->nfor.args = copynode(n->nfor.args);
  256.         break;
  257.       case NCASE:
  258.         new->ncase.cases = copynode(n->ncase.cases);
  259.         new->ncase.expr = copynode(n->ncase.expr);
  260.         break;
  261.       case NCLIST:
  262.         new->nclist.body = copynode(n->nclist.body);
  263.         new->nclist.pattern = copynode(n->nclist.pattern);
  264.         new->nclist.next = copynode(n->nclist.next);
  265.         break;
  266.       case NDEFUN:
  267.       case NARG:
  268.         new->narg.backquote = copynodelist(n->narg.backquote);
  269.         new->narg.text = nodesavestr(n->narg.text);
  270.         new->narg.next = copynode(n->narg.next);
  271.         break;
  272.       case NTO:
  273.       case NFROM:
  274.       case NAPPEND:
  275.         new->nfile.fname = copynode(n->nfile.fname);
  276.         new->nfile.fd = n->nfile.fd;
  277.         new->nfile.next = copynode(n->nfile.next);
  278.         break;
  279.       case NTOFD:
  280.       case NFROMFD:
  281.         new->ndup.dupfd = n->ndup.dupfd;
  282.         new->ndup.fd = n->ndup.fd;
  283.         new->ndup.next = copynode(n->ndup.next);
  284.         break;
  285.       case NHERE:
  286.       case NXHERE:
  287.         new->nhere.doc = copynode(n->nhere.doc);
  288.         new->nhere.fd = n->nhere.fd;
  289.         new->nhere.next = copynode(n->nhere.next);
  290.         break;
  291.       };
  292.       new->type = n->type;
  293.       return new;
  294. }
  295.  
  296.  
  297. STATIC struct nodelist *
  298. copynodelist(lp)
  299.       struct nodelist *lp;
  300.       {
  301.       struct nodelist *start;
  302.       struct nodelist **lpp;
  303.  
  304.       lpp = &start;
  305.       while (lp) {
  306.         *lpp = funcblock;
  307.         funcblock += ALIGN(sizeof (struct nodelist));
  308.         (*lpp)->n = copynode(lp->n);
  309.         lp = lp->next;
  310.         lpp = &(*lpp)->next;
  311.       }
  312.       *lpp = NULL;
  313.       return start;
  314. }
  315.  
  316.  
  317.  
  318. STATIC char *
  319. nodesavestr(s)
  320.       char *s;
  321.       {
  322.       register char *p = s;
  323.       register char *q = funcstring;
  324.       char *rtn = funcstring;
  325.  
  326.       while (*q++ = *p++);
  327.       funcstring = q;
  328.       return rtn;
  329. }
  330.  
  331.  
  332.  
  333. /*
  334.  * Free a parse tree.
  335.  */
  336.  
  337. void
  338. freefunc(n)
  339.       union node *n;
  340.       {
  341.       if (n)
  342.         ckfree(n);
  343. }
  344.